package com.frame.util;


import java.lang.reflect.Array;
import java.util.*;

import com.frame.database.util.ExpandProperty;

public class ConvertFactoy {

	static public Object convertObject(Class c,Object value)
	{
		if(value==null)return null;
		if(c == byte[].class){
			if(value instanceof byte[])
				return value;
			else
				return value.toString().getBytes();
		}else
			return convertValue(c,new String[]{value.toString()});
	}
	/**
	 * 将字符参数转化为各种类型
	 * @param c
	 * @param value
	 * @return
	 */
	static public Object convertValue(Class c,String[] value)
	{
		if(c.isArray() && c != char[].class && c != byte[].class)
		{
			if(value == null)
				return null;

			return convertArray(c.getComponentType(),value);
		}
		else if(c.isAssignableFrom(List.class))
		{
			if(value == null)
				return null;

			return convertList(String.class,value);
		}else if(c==ExpandProperty.class){
			String s = "";
			if(value != null)
				s = value[0];
			return new ExpandProperty(s);
		}
		else
		{
			String s = null;
			if(value != null)
				s = value[0];

			return convertValue(c,s);
		}
	}
	static public Object convertArray(Class c,String[] ss)
	{
		if(c == String.class)
		{
			return ss;
		}
		else
		{
			Object array = Array.newInstance(c,ss.length);
			for(int i = 0; i < ss.length; i++)
			{
				Array.set(array,i,convertValue(c,ss[i]));
			}
			return array;
		}
	}
	static public List convertList(Class c,String[] ss)
	{
		List list = new ArrayList();
		for(int i = 0; i < ss.length; i++)
			list.add(convertValue(c,ss[i]));

		return list;
	}
	static public Boolean convertBoolean(String s)
	{
		if(s == null)
			return null;

		if("true".equals(s.toLowerCase()))
			return Boolean.TRUE;
		else
			return Boolean.FALSE;
	}
	static public Object convertValue(Class c,String s)
	{
		if(s == null)
		{
			if(c.isPrimitive())
			{
				if(c == char.class)
					return new Character(' ');
				else if(c == boolean.class)
					return Boolean.FALSE;
				else
					s = "0";
			}
			else
			{
				return null;
			}
		}

		if(c == String.class)
			return s;

		if("".equals(s))
			return null;

		if(c == Integer.class || c == int.class)
			return new Integer(s);
		else if(c == Long.class || c == long.class)
			return new Long(s);
		else if(c == Short.class || c == short.class)
			return new Short(s);
		else if(c == Byte.class || c == byte.class)
			return new Byte(s);
		else if(c == Float.class || c == float.class)
			return new Float(s);
		else if(c == Double.class || c == double.class)
			return new Double(s);
		else if(c == Character.class || c == char.class)
			return new Character(s.charAt(0));
		else if(c == Boolean.class || c == boolean.class)
			return convertBoolean(s);
		else if(c == Date.class)
			return DateConvert.toDate(s);
		else if(c == java.sql.Time.class)
			return new java.sql.Time(DateConvert.toDate(s).getTime());
		else if(c == java.sql.Timestamp.class)
			return new java.sql.Timestamp(DateConvert.toDate(s).getTime());
		else if(c == java.sql.Date.class)
			return new java.sql.Date(DateConvert.toDate(s).getTime());
		else if(c == char[].class)
			return s.toCharArray();
		else if(c == byte[].class)
			return s.getBytes();
		else
			return null;
	}
}
